
package jminusminus;

import static jminusminus.CLConstants.*;

import java.util.ArrayList;

/**
 * The AST node for a for-statement.
 * 
 * for ( [forInit] ; [expression] ; [forUpdate] ) statement
 */

class JEnhancedForStatement extends JStatement {



	/** forInit statement. */
    private JFormalParameter formalParameter;

    /** Test expression. */
    private JExpression iterator;
    
    /** The Body */
    private JStatement body;

    /**
     * Construct an AST node for a for-statement given its line number, the
     * forInit expression, test expression,forUpdate statement , and the body.
     * 
     * @param line
     *            line in which the while-statement occurs in the source file.
     * @param condition
     *            test expression.
     * @param body
     *            the body.
     */

    public JEnhancedForStatement(int line, JFormalParameter formalParameter, JExpression iterator, JStatement body) {
        super(line);
        this.formalParameter = formalParameter;
        this.iterator = iterator;
        this.body = body;
    }

    // Getters
    
    public JFormalParameter formalParameter() {
		return formalParameter;
	}

	public JExpression iterator() {
		return iterator;
	}

	public JStatement body() {
		return body;
	}
    
    
    /**
     * Analysis involves analyzing the test, checking its type and analyzing the
     * body statement.
     * 
     * @param context
     *            context in which names are resolved.
     * @return the analyzed (and possibly rewritten) AST subtree.
     */

    public JEnhancedForStatement analyze(Context context) {
        body.analyze(context);
        return this;
    }

    /**
     * Generate code for the for loop.
     * 
     * @param output
     *            the code emitter (basically an abstraction for producing the
     *            .class file).
     */

    public void codegen(CLEmitter output) {
    	;
    }

    /**
     * @inheritDoc
     */

    public void writeToStdOut(PrettyPrinter p) {
        p.printf("<JEnhancedForStatement line=\"%d\">\n", line());
        p.indentRight();
        p.printf("<InitialisationStatement>\n");
        p.indentRight();
        formalParameter.writeToStdOut(p);
        p.indentLeft();
        p.printf("</InitialisationStatement>\n");
        p.printf("<IteratorExpression>\n");
        p.indentRight();
        iterator.writeToStdOut(p);
        p.indentLeft();
        p.printf("</IteratorExpression>\n");
        p.printf("<Body>\n");
        p.indentRight();
        body.writeToStdOut(p);
        p.indentLeft();
        p.printf("</Body>\n");
        p.indentLeft();
        p.printf("</JEnhancedForStatement>\n");
    }

}
